package com.ruoyi.projectAch.service.impl;

import java.util.List;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.projectAch.domain.SchoolName;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.projectAch.mapper.GrantingUnitMapper;
import com.ruoyi.projectAch.domain.GrantingUnit;
import com.ruoyi.projectAch.service.IGrantingUnitService;

/**
 * 维护授予单位Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-12
 */
@Service
public class GrantingUnitServiceImpl implements IGrantingUnitService
{
    @Autowired
    private GrantingUnitMapper grantingUnitMapper;

    /**
     * 查询维护授予单位
     *
     * @param id 维护授予单位主键
     * @return 维护授予单位
     */
    @Override
    public GrantingUnit selectGrantingUnitById(Long id)
    {
        return grantingUnitMapper.selectGrantingUnitById(id);
    }

    /**
     * 查询维护授予单位列表
     *
     * @param grantingUnit 维护授予单位
     * @return 维护授予单位
     */
    @Override
    public List<GrantingUnit> selectGrantingUnitList(GrantingUnit grantingUnit)
    {
        return grantingUnitMapper.selectGrantingUnitList(grantingUnit);
    }

    /**
     * 新增维护授予单位
     *
     * @param grantingUnit 维护授予单位
     * @return 结果
     */
    @Override
    public int insertGrantingUnit(GrantingUnit grantingUnit)
    {
        return grantingUnitMapper.insertGrantingUnit(grantingUnit);
    }

    /**
     * 修改维护授予单位
     *
     * @param grantingUnit 维护授予单位
     * @return 结果
     */
    @Override
    public int updateGrantingUnit(GrantingUnit grantingUnit)
    {
        return grantingUnitMapper.updateGrantingUnit(grantingUnit);
    }

    /**
     * 批量删除维护授予单位
     *
     * @param ids 需要删除的维护授予单位主键
     * @return 结果
     */
    @Override
    public int deleteGrantingUnitByIds(Long[] ids)
    {
        return grantingUnitMapper.deleteGrantingUnitByIds(ids);
    }

    /**
     * 删除维护授予单位信息
     *
     * @param id 维护授予单位主键
     * @return 结果
     */
    @Override
    public int deleteGrantingUnitById(Long id)
    {
        return grantingUnitMapper.deleteGrantingUnitById(id);
    }

    @Override
    public String importUser(List<GrantingUnit> grantingUnitList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(grantingUnitList) || grantingUnitList.size() == 0) {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GrantingUnit grantingUnit : grantingUnitList) {
            try {
                if (isUpdateSupport){
                    grantingUnitMapper.updateGrantingUnit(grantingUnit);
                }else {
                    grantingUnitMapper.insertGrantingUnit(grantingUnit);
                }
                successNum++;
            } catch (Exception e) {
                failureNum++;
                e.printStackTrace();
            }
        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确。");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，");
        }
        return successMsg.toString();
    }
}
